package com.fudan.biz.visualize.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fudan.biz.visualize.entity.ChartSetting;
import com.fudan.biz.visualize.service.ChartSettingService;
import com.fudan.framework.base.DaoService;
import com.fudan.framework.base.DataController;
import com.fudan.framework.entity.ResponseMsg;
import com.fudan.framework.entity.TableJson;
import com.fudan.framework.utils.EntityUtils;

@Controller
@RequestMapping("biz/visualize/chart_setting")
//@Permissions("")
public class ChartSettingController extends DataController<ChartSetting> {
    @Autowired
    private ChartSettingService chartSettingService;

    @Override
    protected DaoService<ChartSetting> injectService() {
        return chartSettingService;
    }

    @Override
    protected String pathPrefix() {
        return "biz/visualize";
    }

    @RequestMapping("/chart_setting_list")
    public String showChartSettingList() {
        return "biz/visualize/chart_setting_list";
    }

    @RequestMapping("/list")
    @ResponseBody
    public TableJson list(HttpServletRequest request) {
        List<ChartSetting> limitList = chartSettingService.pages(request);
        return TableJson.build(limitList, chartSettingService.countAll());
    }

    @RequestMapping("/findByChartName")
    @ResponseBody
    public ResponseMsg findByChartName(HttpServletRequest request) throws Exception {
        ChartSetting chartSetting;
        chartSetting = EntityUtils.toEntity(request, ChartSetting.class);
        if (chartSettingService.findByChartName(chartSetting) != null) {
            return ResponseMsg.build(ADD_ERROR_CODE, "名称重复");
        }
        return ResponseMsg.build(SUCCESS_CODE, SUCCESS);
    }

    @RequestMapping("/findByChartId")
    @ResponseBody
    public ResponseMsg findByChartId(HttpServletRequest request) throws Exception {
        Integer chartId = Integer.parseInt(request.getParameter("chartId"));
        ChartSetting chartSetting = chartSettingService.findByChartId(chartId).get();
        if (chartSettingService.findByChartId(chartId).isPresent()) {
            return ResponseMsg.build(ADD_ERROR_CODE, "数据已存在").putData(chartSetting);
        }
        return ResponseMsg.build(SUCCESS_CODE, SUCCESS);
    }

    @RequestMapping("/updateChartById")
    @ResponseBody
    public ResponseMsg updateChartById(HttpServletRequest request) throws Exception {
        ChartSetting chartSetting;
        chartSetting = EntityUtils.toEntity(request, ChartSetting.class);
        Integer integer = chartSettingService.updateChartById(chartSetting);
        if (integer < 1) {
            return ResponseMsg.build(UPDATE_ERROR_CODE, "数据更新失败");
        }
        return ResponseMsg.build(SUCCESS_CODE, SUCCESS);
    }
}
